#!/bin/sh

# _assert_count        $expcnt $*(all_params)
# _assert_non_empty    $name $value
# _assert_type         $name $value "File"|"Folder"
# _assert_range        $name $value "apple,orange,banana"
# _assert_info_on
# _assert_info_off

#############################################################################
export _PRINT_ASSERT_INFO="1"
function _assert_info_on
{
    export _PRINT_ASSERT_INFO="1"
}

function _assert_info_off
{
    export _PRINT_ASSERT_INFO="0"
}

#############################################################################
function _assert_count()
{
    if [ $# -le 1 ]
    then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_count(): at least take 1 param"
        fi
        exit 1
    fi

    exp_cnt="$1"
    if [ -z $exp_cnt ]
    then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_count(): param 0 can not be empty"
        fi
        exit 2
    fi

    let act_cnt=$#-1
    if [ "$exp_cnt" -ne "$act_cnt" ]
    then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_count(): expected to have [$exp_cnt] counts insteald of [$act_cnt],"
            echo "                 which means the func params count was not right."
        fi
        exit 3
    fi
}
# _assert_count 2 1 2 3



#############################################################################
function _assert_non_empty()
{
    #_assert_count 2 $*

    name="$1"
    value="$2"

    if [ -z "$value" ]
    then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_non_empty(): $name can not be empty, quit!"
        fi
        exit 2
    fi
}
#_assert_non_empty "hello" 




#############################################################################
function _assert_type()
{
    _assert_count 3 $*
    
    name="$1"
    value="$2"
    exp_type="$3"
    _assert_non_empty $name $value

    if [ "$exp_type" == "File" ] && [ ! -f "$value" ]; then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_type(): $name = \"$value\", should be a $exp_type"
        fi
        exit 1
    fi
    if [ "$exp_type" == "Folder" ] && [ ! -d "$value" ]; then
        if [ "$_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_type(): $name = \"$value\", should be a $exp_type"
        fi
        exit 5
    fi
}

#_assert_type "FileToFind" "/root/test.sh" "Folder"



#############################################################################
function _assert_range()
{
    _assert_count 3 $*

    name="$1"
    value="$2"
    val_list="$3"
    _assert_non_empty $name $value

    mchval=`echo $val_list | awk -F ',' '{\
        for (i=1;i<=NF;i++) \
        {\
            if ( $i == "'"$value"'") \
            {\
                print $i\
            }\
        }\
        }'`
    if [ -z "$mchval" ]; then
        if [ "_PRINT_ASSERT_INFO"=="1" ]
        then
            echo "_assert_range(): $name = \"$value\", not in range list {$val_list}"
        fi
        exit 1
    fi

}
#_assert_range "fruit" "apple" "apple,orange,banana"




